package com.to.aboomy.bannersample.slice;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.enrique.stackblur.StackBlurManager;
import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.HolderCreator;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.indicator.BannerBgContainer;
import com.to.aboomy.bannersample.indicator.BannerBgView;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.util.ZoomOutSlideTransformer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.List;

/**
 * 转转样式
 */
public class RevealBannerAbilitySlice extends AbilitySlice implements HolderCreator {
    BannerBgContainer bannerBgContainer;
    Banner banner;
    List<Integer> data;
    float reduceValue = 0.2f;
    float upValue = 2.5f;
    PageSlider view_page;
    boolean isFastSliding = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_reveal_banner);

        bannerBgContainer = (BannerBgContainer) findComponentById(ResourceTable.Id_banner_bg_container);
        banner = (Banner) findComponentById(ResourceTable.Id_banner);
        view_page = (PageSlider) findComponentById(ResourceTable.Id_viewPager);
        initBanner1();
    }

    private void initBanner1() {
        banner.setHolderCreator(this);
        banner.setAutoPlay(true);
        banner.setPagerScrollDuration(2000);
        banner.setIndicator(new IndicatorView(this).setIndicatorColor(Color.WHITE).setIndicatorSelectorColor(Color.WHITE)
                .setIndicatorStyle(IndicatorView.INDICATOR_DASH))
                .setPageTransformer(true, new ZoomOutSlideTransformer())
                .setPageMargin(0, 100);

        data = Utils.getData(5);
        bannerBgContainer.setBannerBackBg(this, data);
        banner.setPages(data);
        banner.setOnPageClickListener(position -> {
            ToastUtil.showToast(getAbility(), position + "");
        });
        getBgBackItemPos();

        getUITaskDispatcher()
                .delayDispatch(new Runnable() {
                                   @Override
                                   public void run() {
                                       banner.setCurrentPage(1);
                                   }
                               }
                        , 10);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    public Component createView(Context context, int integer, int index) {

        Component component = LayoutScatter.
                getInstance(context).
                parse(ResourceTable.Layout_item_banner_image, null, false);
        component.setMarginLeft(100);
        Image image = (Image) component.findComponentById(ResourceTable.Id_img);
        Glide.with(context).load(integer)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(true)
                .transform(new RoundedCorners(10))
                .into(image);
        return component;
    }

    /**
     * 切换banner 修改背景
     */
    private void getBgBackItemPos() {
        banner.setOuterPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
                BannerBgView bannerBgView;
                if (itemPosOffsetPixels > 1000) {
                    if (!isFastSliding) {
                        isFastSliding = true;
                    }
                }
                if (itemPosOffsetPixels > 0 && isFastSliding) {
                    if (itemPos == data.size() - 1) {
                        itemPos = 0;
                    }else{
                        itemPos++;
                    }
                    bannerBgView = bannerBgContainer.getBannerBgViews().get(itemPos);
                    bannerBgContainer.moveChildToFront(bannerBgView);

                    if (bannerBgView.getImageView().getPixelMapHolder() != null) {
                        PixelMap pixelMap = bannerBgView.getImageView().getPixelMapHolder().getPixelMap();
                        if (pixelMap != null) {
                            StackBlurManager stackBlurManager = new StackBlurManager(pixelMap);
                            pixelMap = stackBlurManager.process((int) (0.25f * 100));
                            bannerBgView.getImageView().setPixelMap(pixelMap);
                        }
                    }
                    bannerBgView.setImageHolder(bannerBgView.getImageView().getPixelMapHolder());
                    bannerBgView.hideClipAnimation((float) (((double) itemPosOffset - reduceValue) * upValue), itemPosOffsetPixels);
                } else {
                    itemPos--;
                    if (itemPos < 0) {
                        itemPos = data.size() - 1;
                    }
                    bannerBgView = bannerBgContainer.getBannerBgViews().get(itemPos);
                    bannerBgContainer.moveChildToFront(bannerBgView);

                    if (bannerBgView.getImageView().getPixelMapHolder() != null) {
                        PixelMap pixelMap = bannerBgView.getImageView().getPixelMapHolder().getPixelMap();
                        if (pixelMap != null) {
                            StackBlurManager stackBlurManager = new StackBlurManager(pixelMap);
                            pixelMap = stackBlurManager.process((int) (0.25f * 100));
                            bannerBgView.getImageView().setPixelMap(pixelMap);
                        }
                    }

                    bannerBgView.setImageHolder(bannerBgView.getImageView().getPixelMapHolder());
                    bannerBgView.hideClipAnimation((float) (((double) itemPosOffset - reduceValue) * upValue), itemPosOffsetPixels);
                }
            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
